# -*- coding: utf-8 -*-
'''
Created on 2010/07/06

@author: nagai
'''

from kay.utils import forms
from kay.utils.forms.modelform import ModelForm

from kay.i18n import gettext as _
from kay.utils import render_to_response
from kay.auth.decorators import login_required, admin_required
from kay.utils import create_logout_url
from google.appengine.ext import db
from google.appengine.api import images
from datetime import datetime
from kay.utils import set_trace

from werkzeug import (
  unescape, redirect, Response,append_slash_redirect
)

from webfolio.models import Card, Portfolio, Group


class PortfolioAddForm(forms.Form):
    title = forms.TextField(required=True)
    subtitle = forms.TextField(widget=forms.Textarea)
    cards = forms.MultiChoiceField()
    #TODO:
    #group = forms.ModelField(model=Group, required=True)

    
class PortfolioEditForm(forms.Form):
    key = forms.TextField(required=True, widget=forms.HiddenInput) 
    title = forms.TextField(required=True)
    subtitle = forms.TextField(widget=forms.Textarea)
    cards = forms.MultiChoiceField()
    #TODO:
    #group = forms.ModelField(model=Group, required=True)
    
    
@login_required
def portfolio_add(request):
    form = PortfolioAddForm()
    card_query = Card.gql("WHERE author = :1 ORDER BY created ASC", request.user)
    cs = card_query.fetch(1000)
    form.cards.choices = [(x.key(), x.title) for x in cs]
    if request.method == "POST":
        set_trace
        if form.validate(request.form):
            pf = Portfolio(title=form['title'],     
                        )
            if request.user.is_authenticated():
                pf.author = request.user
            pf.title = form['title']
            pf.subtitle = form['subtitle']
            pf.cards = form['cards']
            #TODO:
            #pf.group = form['group']                      
            pf.put()
            return redirect("/")
    return render_to_response("webfolio/add.html", {"form": form.as_widget()})

@login_required
def portfolio_edit(request):
    form = PortfolioEditForm()
    card_query = Card.gql("WHERE author = :1 ORDER BY created ASC", request.user)
    cs = card_query.fetch(1000)
    form.cards.choices = [(x.key(), x.title) for x in cs]
    if request.method == "POST":
        if form.validate(request.form):
            if form['key']:
                pf = db.get(form['key'])
                pf.title = form['title']
                pf.subtitle = form['subtitle']
                pf.cards = form['cards']
                #TODO:
                #pf.group = form['group']                      
                pf.modified = datetime.now()
                pf.put()
            return redirect("/")
    elif request.method == "GET":
        """"""
        key = request.values['key']
        pf = db.get(key)
        widget = form.as_widget()
        widget['key']._value = pf.key()
        widget['title']._value = pf.title
        widget['subtitle']._value = pf.subtitle
        widget['cards']._value = pf.cards
        #TODO:
        #widget['group']._value = pf.group
        
    return render_to_response("webfolio/edit.html", {"form": widget})
